//
//  TopicTwoCell.m
//  SweetComics
//
//  Created by LeeZhe on 15/2/1.
//  Copyright (c) 2015年 LeeZhe. All rights reserved.
//

#import "TopicTwoCell.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#define kURLFour @"http://mobilev3.ac.qq.com/Index/homeTopicListForIos/clientType/0/local_version/1/index/3"
@implementation TopicTwoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)awakeFromNib {
    // Initialization code
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    self.contentView.frame = CGRectMake(7, 0, self.frame.size.width, self.frame.size.height);
}
-(void)setComics:(ComicList *)comics
{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:kURLFour parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        [comics setValuesForKeysWithDictionary:dic];
        for (NSInteger i = 0;  i < comics.data.count; i++) {
            NSString *str = [[comics.data objectAtIndex:i] objectForKey:@"topic_url"];
            NSURL *url = [NSURL URLWithString:str];
            self.topicView = [[UIImageView alloc] initWithFrame:CGRectMake(i % 2 * (0.493 * self.contentView.frame.size.width),i / 2 * 100 + 10,0.467 * self.contentView.frame.size.width,90)];
            [_topicView setImageWithURL:url];
            [self.contentView addSubview:_topicView];
            [_topicView release];
        }
        
        
        // 添加一个通知中心 把 comics 对象传给 首页
        [[NSNotificationCenter defaultCenter] postNotificationName:@"topicTwo" object:comics];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];

    
}
- (void)dealloc
{
    [_topicView release];
    [_button release];
    [_comics release];
    [super dealloc];

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
